<?php
namespace app\controller;

use think\facade\Db;
use think\Request;
use think\facade\View;
use think\helper\Str;
use think\exception\ValidateException;


// 测试课件预览
class Preview
{
    /**
     * 生成预览二维码
     * @date: 2019年10月8日
     * @author: Administrator
     * @return:
     */
    public function createQrcode(Request $request)
    {

        $id = $request->param('id/d',0);

        require_once '../extend/phpqrcode/QRcode.php';

        $data = $request->domain() . '/pre/index.html?id='. $id;


        $path = '/preview/'.$id.'.png';

        $outfile = public_path().$path;

        $level = 'L';
        $size = 12;
        $QRcode = new \phpqrcode\QRcode();

        ob_start();
        $QRcode->png($data,$outfile,$level,$size,2);
        ob_end_clean();

        return json(['code'=>1,'data'=>$path,'msg'=>'']);
    }


    /**
     * 预览页面
     * @date: 2019年10月8日
     * @author: Administrator
     * @return:
     */
    public function test(Request $request)
    {

        View::assign('id', $request->param('id/d',0));
        return View::fetch();
    }

    /**
     * 问题列表
     * @date: 2019年10月8日
     * @author: Administrator
     * @return:
     */
    public function questionList(Request $request)
    {


        $test_id = $request->param('id/d',0);

        if ($test_id <= 0) {
            return json(['code'=>500,'data'=>[],'msg'=>'error id']);
        }
        //test
        $step_test = Db::name('courseware_test')->where('id', '=', $test_id)->find();
        if ($test_id <= 0) {
            return json(['code'=>500,'data'=>[],'msg'=>'no that data']);
        }
        $step_question = Db::name('courseware_test_question')->where('test_id', '=',$step_test['id'])->select()->toArray();

        foreach ($step_question as $key => $value) {

            $arr = json_decode($value['answer'],true);

            foreach ($arr as $k => $v) {
                $arr[$k]['t'] = htmlspecialchars_decode($v['t']);
            }

            $step_question[$key]['answer'] = $arr;
        }

        $step_test['question'] = $step_question;

        return json(['code'=>200,'data'=>$step_test,'msg'=>'ok']);
    }
}